let axios = require("axios");
let fs = require("fs");

// banner 图片资源链接
const HOME_BANNER_URL = "https://c.y.qq.com/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg";

// 热词数据资源链接
const HOME_HOT_KEY_URL = "https://c.y.qq.com/splcloud/fcgi-bin/gethotkey.fcg?g_tk_new_20200303=2060565456&g_tk=2060565456&loginUin=1848661762&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0";

// 搜索数据资源链接
let HOME_SEARCH_KEY_URL = `https://c.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg?is_xml=0&key=$$_$$&g_tk_new_20200303=2060565456&g_tk=2060565456&loginUin=1848661762&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0`;

// 获取首页 banner 图片数据
let getHomeBannerDataService = function getHomeBannerDataService(fn) {
    axios.get(HOME_BANNER_URL).then((res) => {
        console.log(res.data.data);

        fn(res.data.data.slider.map(item => ({
            imgUrl: item.picUrl,
            id: item.id,
        })));
    });




};

// 获取首页推荐歌单数据
let getHomeSongsSheetDataService = function getHomeSongsSheetDataService(fn) {
    // 读取本地数据
    fs.promises.readFile('./src/source/lxSiteInfo.json').then(data => {
        let localData = JSON.parse(data.toString());
        localData = localData.recomPlaylist.data.v_hot.slice(0, 5).map(item => ({
            cover: item.cover,
            introduce: item.title,
            playerNum: item.listen_num,
            id: item.content_id
        }));
        fn(localData);
    }, err => {
        console.log('数据读取失败');
    });
};

// 获取首页精选推荐数据
let getHomeSelectedActivityDataService = function getHomeSelectedActivityDataService(fn) {
    // 读取本地数据
    fs.promises.readFile('./src/source/lxSiteInfo.json').then(data => {
        let localData = JSON.parse(data.toString());

        fn(localData.focus);
    }, err => {
        console.log('数据读取失败');
    });
};

// 获取首页排行榜数据
let getHomeTopListDataService = function getHomeTopListDataService(fn) {
    // 读取本地数据
    fs.promises.readFile('./src/source/lxSiteInfo.json').then(data => {
        let localData = JSON.parse(data.toString()).toplist.data.group;
        let arr = [];
        localData.forEach(item => {
            arr.push(...item.toplist);
        });


        let temp = [];
        while (temp.length < 5) {

            let num = Math.floor(Math.random() * arr.length);
            let ele = arr.splice(num, 1);
            temp.push(...ele);
        }
        // console.log(temp);
        temp = temp.map(item => ({
            cover: item.frontPicUrl,
            song: item.song.map(item => ({
                id: item.albumMid,
                singerMid: item.singerMid,
                singerName: item.singerName,
                title: item.title,
            })),
            title: item.title,
        }));

        fn(temp);
    }, err => {
        console.log('数据读取失败');
    });
};

// 获取热词数据库

let getHomeHotWordService = function getHomeHotWordService(fn) {
    // 请求网络数据
    axios.get(HOME_HOT_KEY_URL).then(res => {
        let data = res.data.data.hotkey;
        data = data.slice(0, 5).map(item => ({
            hotWord: item.k,
            id: item.n,
        }));
        fn(data);
    }).catch(err => {
        console.log(err.message);
    });
};

let getHomeSearchWordService = function getHomeSearchWordService(fn, params) {
    console.log('################## 269172812 --');

    let url = HOME_SEARCH_KEY_URL;

    console.log(HOME_SEARCH_KEY_URL);
    let sign = () => {
        return typeof word === "string" && /^\s*$/.test(word) ? encodeURI("国歌") : encodeURI(params.searchText);
    };
    url = url.replace("$$_$$", sign);
    axios.get(url).then(res => {

        console.log(res.data.data.song);
        let data = res.data.data.song.itemlist.map(item => ({
            id: item.id,
            mid: item.mid,
            name: item.name,
            singer: item.singer,
        }));

        console.log(data);
        fn(data);

    }).catch(err => {
        console.log(err.message);
    });
};

module.exports = {
    getHomeBannerDataService,
    getHomeSongsSheetDataService,
    getHomeSelectedActivityDataService,
    getHomeTopListDataService,
    getHomeHotWordService,
    getHomeSearchWordService,
};